import os
import shutil

OUTPUT = 'output'

def main():
    files = os.listdir(get_dir())
    for inx, d in enumerate(files):
        print('[%s]%s' % (inx, d))
    while True:
        print('输入文件序号(空行退出)：')
        line = input()
        if line.strip() == '':
            break
        file_inx = -1
        try:
            file_inx = int(line.strip())
            if file_inx < 0 or file_inx >= len(files):
                raise ValueError()
        except:
            continue

        print('输入要复制的份数(例如：abc.txt，复制2份，为abc1.txt,abc2.txt):')
        line = input()
        if not os.path.isdir(os.path.join(get_dir(), OUTPUT)):
            os.mkdir(os.path.join(get_dir(), OUTPUT))
        try:
            num = int(line.strip())
            if num < 0:
                print('请输入大于0的整数')
        except:
            continue
        
        print('请输入固定数字位数(空行为0)，例如abc1.txt（不用0补位）还是abc01.txt（补1位）')
        line = input()
        mask = 0
        if not line.strip() == '':
            try:
                mask = int(line)
                if mask < 0 or mask > 5:
                    raise ValueError()
            except:
                continue
        
        print('数字从？开始计数（空行为1）：')
        line = input()
        start = 1
        if not line.strip() == '':
            try:
                start = int(line)
                if start < 0:
                    raise ValueError()
            except:
                continue

        filepath_src = os.path.join(get_dir(), files[file_inx])
        count = 0
        for seed in range(start, num + start):
            filepath_tar = next_filename(files[file_inx], seed, mask)
            # print(filepath_src, '->', filepath_tar)
            shutil.copy(filepath_src, filepath_tar)
            count += 1

        print('【复制成功】 %s 个文件到 /%s' % (count, OUTPUT))

def next_filename(oldname, seed = 1, mask = 0):
    [filename, ext] = os.path.splitext(oldname)

    if not mask == 0:
        seed = 100000000 + seed
        seed = str(seed)[-1 * mask:]
    return os.path.join(get_dir(), OUTPUT, '%s%s%s' % (filename, seed, ext))


def get_dir():
    return os.path.dirname(os.path.realpath(__file__))

main()